package com.frogs.ucenter.controller;

import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.entity.UserFamilyMember;
import com.frogs.ucenter.service.TagService;
import com.frogs.ucenter.service.UserFamilyMemberService;

@Controller
public class UserFamilyMemberController extends BaseController3 {
	private static Logger log = LoggerFactory.getLogger(UserFamilyMemberController.class);    
	
	private static final String EDIT_VIEW = "familyMember/edit";
	private static final String HOME_VIEW = "familyMember/index";
	
	private static final String EDIT = "/familyMember/edit";
	private static final String HOME = "/familyMember/index";
	
	private static final String TAG_LIST = "/familyMember/tagList";
	private static final String INFO = "/familyMember/info";
	private static final String LIST = "/familyMember/list";
	private static final String ADD = "/familyMember/add";
	private static final String DELETE = "/familyMember/delete";
	private static final String UPDATE = "/familyMember/update";
	
	@Resource
	private UserFamilyMemberService userFamilyMemberService;
	@Resource
	private TagService tagService;
	@Resource
	private UserPermissionHelper userPermissionHelper;
	
    @RequestMapping(HOME)
    public String listUserFamilyMember(HttpServletRequest request, ModelMap model) {
		
		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
		
		try{
			JSONObject json = userFamilyMemberService.getFamilyMemberList(user.getId());
			JSONArray ja = json.getJSONObject("response").getJSONArray("list");
			for(int i=0;i<ja.size(); i++){
				String testJsonStr = ja.getJSONObject(i).getString("diseaseTag");
				ja.getJSONObject(i).put("diseaseTaglist", JSONObject.parseObject(testJsonStr));
			}
			model.put("list", json.getJSONObject("response").getJSONArray("list"));
			model.put("userName", user.getAccountName());
		}catch (Exception e) {
			
			log.error("func[listUserFamilyMember] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String editFamilyMember(HttpServletRequest request, ModelMap model) {
    	try {
			return EDIT_VIEW;
		} catch (Exception e) {
			log.error("func[editFamilyMember] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return ERROR_VIEW;
    }
    
    
	@RequestMapping(TAG_LIST)
	public JSONObject getTagListBySexaAge(HttpServletRequest request,ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			
			int age = requestInfo.getIntParameter("age");
			int sex = requestInfo.getIntParameter("sex");
			return tagService.getTagListBySexaAge(sex, age);
		} catch (Exception e) {
			log.error("func[getTagListBySexaAge] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	@RequestMapping(INFO)
	public JSONObject getFamilyMemberInfo(HttpServletRequest request,ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			
			int familyMemberId = requestInfo.getIntParameter("familyMemberId");
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			return userFamilyMemberService.getFamilyMemberInfo(familyMemberId,user.getId());
		} catch (Exception e) {
			log.error("func[getFamilyMemberInfo] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
    @RequestMapping(LIST)
    public JSONObject listFamilyMember(HttpServletRequest request, ModelMap model) {
		try {
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);	
			
			return userFamilyMemberService.getFamilyMemberList(user.getId());

		} catch (Exception e) {
			log.error("func[listFamilyMember] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
    }
    
    @RequestMapping(ADD)
    public JSONObject addFamilyMember(HttpServletRequest request, ModelMap model) {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			String relatives = requestInfo.getParameter("relatives");
			int sex = requestInfo.getIntParameter("sex");
			String diseaseTag = requestInfo.getParameter("diseaseTag");
			int age = requestInfo.getIntParameter("age");
			UserFamilyMember userFamilyMember = new UserFamilyMember();
			userFamilyMember.setUserId(user.getId());
			userFamilyMember.setRelatives(relatives);
			userFamilyMember.setSex(sex);
			if(null==diseaseTag){
				diseaseTag = "";
			}
			userFamilyMember.setDiseaseTag(diseaseTag);
			userFamilyMember.setAge(age);
			
			return userFamilyMemberService.addFamilyMember(userFamilyMember, requestInfo.getUserIp());
		} catch (Exception e) {
			log.error("func[addFamilyMember] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
    }
    
    @RequestMapping(DELETE)
    public JSONObject deleteFamilyMember(HttpServletRequest request, ModelMap model) throws Exception {
		try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			int familyMemberId = requestInfo.getIntParameter("familyMemberId");
			
			return userFamilyMemberService.deleteFamilyMember(familyMemberId,user.getId());

		} catch (Exception e) {
			log.error("func[deleteFamilyMember] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
    }
    @RequestMapping(UPDATE)
    public JSONObject updateFamilyMember(HttpServletRequest request, ModelMap model) throws Exception {
    	try {
			HttpRequestInfo requestInfo = new HttpRequestInfo(request);
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			UserFamilyMember userFamilyMember = new UserFamilyMember();
			int type = requestInfo.getIntParameter("type");
			if(type==1){//删除一个子标签
			}else{//修改用户信息
				int age = requestInfo.getIntParameter("age");
				int sex = requestInfo.getIntParameter("sex");
				String relatives = requestInfo.getParameter("relatives");
				userFamilyMember.setAge(age);
				userFamilyMember.setRelatives(relatives);
				userFamilyMember.setSex(sex);
			}
			int familyMemberId = requestInfo.getIntParameter("familyMemberId");
			String diseaseTag = requestInfo.getParameter("diseaseTag");
			if(null==diseaseTag){
				diseaseTag = "";
			}
			userFamilyMember.setUserId(user.getId());
			userFamilyMember.setDiseaseTag(diseaseTag);
			userFamilyMember.setId(familyMemberId);
			return userFamilyMemberService.updateFamilyMember(userFamilyMember);
    		
    	} catch (Exception e) {
    		log.error("func[deleteFamilyMember] parameterMap[{}] exception[{} - {}] desc[fail]",//
    				new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
    	}
    	return null;
    }
}
	
	
	